﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class useSpriteImage : Image
{

    protected override void OnPopulateMesh(VertexHelper toFill)
    {
        base.OnPopulateMesh(toFill);

        List<UIVertex> uIVertices = new List<UIVertex>();

        toFill.GetUIVertexStream(uIVertices);

        Vector2 leftBottom = uIVertices[0].position;
        Vector2 rightTop = uIVertices[2].position;

        Vector2 imageoffset = new Vector2(leftBottom.x- rightTop.x,leftBottom.y-rightTop.y);

        Vector2 offset = new Vector2(imageoffset.x /sprite.bounds.size.x,imageoffset.y/sprite.bounds.size.y);

        uIVertices.Clear();

        UIVertex uIVertex;
        for (int i = 0; i < sprite.vertices.Length; i++)
        {
            uIVertex = new UIVertex();
            uIVertex.position = new Vector3(sprite.vertices[i].x*offset.x,sprite.vertices[i].y*offset.y);
            uIVertex.color = color;
            uIVertex.uv0 = sprite.uv[i];
            uIVertices.Add(uIVertex);
        }

        List<int> triangles = new List<int>();

        for (int i = 0; i < sprite.triangles.Length; i++)
        {
            triangles.Add(sprite.triangles[i]);
        }

        toFill.Clear();
        toFill.AddUIVertexStream(uIVertices, triangles);

    }
}
